home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / eulisp / feel-075.lha / feel0.75 / AddOns / feel_malloc.c < prev    next >
C/C++ Source or Header  |  1992-06-19  |  2KB  |  94 lines

  1. /* 
  2.  * Eulisp version of malloc
  3.  * may not be safe...
  4.  */
  5. #include "defs.h"
  6. #include "structs.h"
  7. #include "allocate.h"
  8.  
  9. #define PAGBITS 5
  10. #define MAXHEAPLEN 16384
  11. #define BIGINT 0xffffffff
  12.  
  13. #ifdef CGC
  14. char *feel_malloc(int size)
  15. {
  16.    return (char *)gc_malloc(size);
  17. }
  18.  
  19. void feel_free(char *data)
  20. {
  21.   gc_free(data);
  22. }
  23. #else
  24. char *feel_malloc(int size)
  25. {
  26.   char *data;
  27.   int len;
  28.  
  29.   char *ptr;
  30.    /* keep the numbers round */
  31.  
  32.   len = (((size + 2*sizeof(int)) >> PAGBITS) << PAGBITS) + (1 << PAGBITS);
  33.  
  34. #define debug
  35. #ifdef debug /* Tue Apr 23 16:45:03 1991 */
  36. /**/  if (len < size)
  37. /**/  {
  38. /**/    fprintf(stderr,"Feel-malloc bug sz: %d len:%d \n",size,len);
  39. /**/    exit(0);
  40. /**/  }
  41. #endif /* debug Tue Apr 23 16:45:03 1991 */
  42.  
  43.   if (len < MAXHEAPLEN)
  44.     data = allocate_space(NULL,len);
  45.   else
  46.     data = allocate_stack(NULL,len);
  47.  
  48.  
  49. #define notdef
  50. #ifdef notdef /* Tue Apr 23 16:45:29 1991 */
  51. /**/  fprintf(stderr,"Malloc: allocating %x bytes at %x\n",len,data+4);  
  52. #endif /* notdef Tue Apr 23 16:45:29 1991 */
  53.  
  54.   if(data == NULL)
  55.     {
  56.       fprintf(stderr,"Malloc failed...panicing. Request was for %d(%d) bytes.",size,len);
  57.       exit(0);
  58.     }
  59.  
  60.   *((int *) (data + len - sizeof(int))) = 0xdeadbeef;
  61.   *(int *) data = len;
  62.  
  63. #ifdef notdef /* Tue Apr 23 16:45:48 1991 */
  64. /**/  for (ptr = data + sizeof(int) ; ptr < data + len - sizeof(int) ; ptr ++)
  65. /**/    *ptr='x';
  66. #endif /* notdef Tue Apr 23 16:45:48 1991 */
  67.  
  68.   return (data + sizeof(int));
  69. }
  70.  
  71.  
  72. void feel_free(char *data)
  73. {
  74.   int len;
  75. #define notdef
  76. #ifdef notdef /* Tue Apr 23 16:46:23 1991 */
  77. /**/  printf("Malloc: freeing: %x bytes at %x\n",*((int *) (data-4)),data);
  78. /**/  if ( *((int *) (data + len - 2*sizeof(int))) != 0xdeadbeef)
  79. /**/    {
  80. /**/      fprintf(stderr,"DataSpace overrun: 0x%x\n",data);
  81. /*      exit(255);*/
  82. /**/    }
  83. #endif /* notdef Tue Apr 23 16:46:23 1991 */
  84.   len = *((int *) (data - sizeof(int)));
  85.   
  86.   if (len < MAXHEAPLEN) /* Null is stack pointer --- pray for no errors */
  87.     deallocate_space(NULL,data - sizeof(int), len);
  88.   else
  89.     deallocate_stack(NULL,data - sizeof(int), len);
  90.   
  91.   return;
  92. }
  93. #endif
  94.